package com.one.blocks.mvc.manager;

import com.one.blocks.mvc.mapper.BaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author linfuxin Created on 2022-02-14 11:32:05
 */
@Slf4j
public abstract class AbstractManager<Mapper extends BaseMapper<T>, T> implements BaseManager<T> {

    protected final Mapper mapper;

    public AbstractManager(Mapper mapper) {
        this.mapper = mapper;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(T entity) {
        mapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Serializable id) {
        mapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(Collection<? extends Serializable> ids) {
        mapper.deleteByIds(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(T entity) {
        mapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelectiveById(T entity) {
        mapper.updateSelectiveById(entity);
    }

    @Override
    public T get(Serializable id) {
        return mapper.selectById(id);
    }

    @Override
    public List<T> getByIds(Collection<? extends Serializable> idList) {
        return mapper.selectByIds(idList);
    }

    @Override
    public T findOne(T param) {
        return mapper.selectOne(param);
    }

    @Override
    public List<T> find(T entity) {
        return mapper.selectByEntity(entity);
    }
}
